package graph_viewer.structure;

import java.awt.Graphics;
import java.util.*;

import graph_viewer.canvas.*;
import graph_viewer.util.*;

public class Abridgement implements Visual_Component{
	ArrayList			the_abridgement_nodes;
	Abridgement_Node	the_root;

	public Abridgement(){}
	public Abridgement( Graph a_graph ){
		Debug.println( "Start to build Abridgement.", 40 );

		Node the_graph_root	= a_graph.find_tree().get_root();
		Debug.println( "\tStart to build the_root.", 40 );
		set_root( new Abridgement_Node( the_graph_root ) );	
		
		Debug.println( "\tStart to build_abridgement_nodes.", 40 );
		set_abridgement_nodes( build_abridgement_nodes( a_graph) );

		Debug.println( "Abridgement is built.", 40 );

	}
	
	public ArrayList find_abridgement_nodes_labels(){
		ArrayList abridgement_nodes_labels = new ArrayList();

		for ( Iterator _i = the_abridgement_nodes.iterator(); _i.hasNext(); ){
			Abridgement_Node a_n	= (Abridgement_Node)_i.next();

			String label	= a_n.get_label();

			abridgement_nodes_labels.add( label );
		}

		return abridgement_nodes_labels;
	}
	
	public Abridgement_Node search( String a_label ){
		for ( Iterator _i = the_abridgement_nodes.iterator(); _i.hasNext(); ){
			Abridgement_Node a_n = (Abridgement_Node)_i.next();

			if ( a_n.get_label().equals( a_label ) ){
				return a_n;
			}
		}

		return null;
	}
	
	public String toString(){
		StringBuffer abridgment = new StringBuffer();
		abridgment.append( "\nThe abridgment " );
		abridgment.append( "Root is: " + the_root.toString() );
		abridgment.append( "the_abridgement_nodes:\n" );
		for ( Iterator _i = the_abridgement_nodes.iterator(); _i.hasNext(); ){
			Abridgement_Node a_n	= (Abridgement_Node)_i.next();

			abridgment.append( a_n.toString() );
		}

		return abridgment.toString();
	}

	//============================================================================
	// setter and getter
	//============================================================================	
	public void set_root( Abridgement_Node a_root ){
		the_root	= a_root;	
	}

	public Abridgement_Node get_root(){
		return the_root;
	}

	public void set_abridgement_nodes( ArrayList a_abridgement_nodes ){
		the_abridgement_nodes	 = a_abridgement_nodes;
	}

	public ArrayList get_abridgement_nodes(){
		return the_abridgement_nodes;
	}
	
	//============================================================================
	// private
	//============================================================================
	private ArrayList build_abridgement_nodes( Graph a_graph ){
		Debug.println( "Start to build abridgment nodes.", 40 );
		ArrayList an_abridgement_nodes	= new ArrayList();

		for ( Iterator _i = a_graph.find_nodes().iterator(); _i.hasNext(); ){
			Node a_graph_node	= (Node)_i.next();

			if ( a_graph_node.is_displayed() ){
				an_abridgement_nodes.add( new Abridgement_Node( a_graph_node ) );
			}
		}
		
		Debug.println( "Finish build abridgment nodes." + an_abridgement_nodes, 40 );

		return an_abridgement_nodes;
	}
}

